//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.number;

import java.util.ArrayList;
import java.util.Iterator;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.util.MEDate;

// Referenced classes of package org.jpgolay.me.number:
//            Number
public class NextNumber {

    public NextNumber() {
        crud = new ObjectCRUD();
    }

    public String getSendingNumber() {
        MEDate date = new MEDate();
        Number nb = null;
        String thisTime = date.getMoisAnnee();
        java.util.Collection c = crud.readAll(null, 35);
        if (c != null) {
            ArrayList list = new ArrayList(c);
            Iterator it = list.iterator();
            it.hasNext();
            nb = (Number) it.next();
            String lastTime = nb.getTime();
            if (!lastTime.equals(thisTime)) {
                crud.delete(null, null, 35);
            }
            Integer pk = (Integer) crud.create(35, null);
            nb = (Number) crud.read(pk, 35);
            if (nb != null) {
                nb.setTime(thisTime);
                crud.update(nb, 35);
            }
            return thisTime + pk.toString();
        } else {
            return null;
        }
    }

    public String getBatchNumber() {
        MEDate date = new MEDate();
        Number nb = null;
        String thisTime = date.getMoisAnnee();
        java.util.Collection c = crud.readAll(null, 37);
        if (c != null) {
            ArrayList list = new ArrayList(c);
            Iterator it = list.iterator();
            it.hasNext();
            nb = (Number) it.next();
            String lastTime = nb.getTime();
            if (!lastTime.equals(thisTime)) {
                crud.delete(null, null, 37);
            }
            Integer pk = (Integer) crud.create(37, null);
            nb = (Number) crud.read(pk, 37);
            if (nb != null) {
                nb.setTime(thisTime);
                crud.update(nb, 37);
            }
            return thisTime + pk.toString();
        } else {
            return null;
        }
    }

    public String getSalePieceNumber() {
        MEDate date = new MEDate();
        Number nb = null;
        String thisTime = date.getMoisAnnee();
        java.util.Collection c = crud.readAll(null, 38);
        if (c != null) {
            ArrayList list = new ArrayList(c);
            Iterator it = list.iterator();
            it.hasNext();
            nb = (Number) it.next();
            String lastTime = nb.getTime();
            if (!lastTime.equals(thisTime)) {
                crud.delete(null, null, 38);
            }
            Integer pk = (Integer) crud.create(38, null);
            nb = (Number) crud.read(pk, 38);
            if (nb != null) {
                nb.setTime(thisTime);
                crud.update(nb, 38);
            }
            return thisTime + pk.toString();
        } else {
            return null;
        }
    }

    public String getBillPieceNumber() {
        MEDate date = new MEDate();
        Number nb = null;
        String thisTime = date.getMoisAnnee();
        java.util.Collection c = crud.readAll(null, 36);
        if (c != null) {
            ArrayList list = new ArrayList(c);
            Iterator it = list.iterator();
            it.hasNext();
            nb = (Number) it.next();
            String lastTime = nb.getTime();
            if (!lastTime.equals(thisTime)) {
                crud.delete(null, null, 36);
            }
            Integer pk = (Integer) crud.create(36, null);
            nb = (Number) crud.read(pk, 36);
            if (nb != null) {
                nb.setTime(thisTime);
                crud.update(nb, 36);
            }
            return thisTime + pk.toString();
        } else {
            return null;
        }
    }

    public String getBuyPieceNumber() {
        MEDate date = new MEDate();
        Number nb = null;
        String thisTime = "f" + date.getMoisAnnee();
        java.util.Collection c = crud.readAll(null, 34);
        if (c != null) {
            ArrayList list = new ArrayList(c);
            Iterator it = list.iterator();
            it.hasNext();
            nb = (Number) it.next();
            String lastTime = nb.getTime();
            if (!lastTime.equals(thisTime)) {
                crud.delete(null, null, 34);
            }
            Integer pk = (Integer) crud.create(34, null);
            nb = (Number) crud.read(pk, 34);
            if (nb != null) {
                nb.setTime(thisTime);
                crud.update(nb, 34);
            }
            return thisTime + pk.toString();
        } else {
            return null;
        }
    }
    private ObjectCRUD crud;
}
